package com.chixing.ih.module.system.service.Impl;

import com.chixing.ih.module.programmer.mapper.ImageMapper;
import com.chixing.ih.module.programmer.mapper.ProCommunityMapper;
import com.chixing.ih.module.programmer.mapper.WorkMapper;
import com.chixing.ih.module.programmer.pojo.UserImage;
import com.chixing.ih.module.system.mapper.UserMapper;
import com.chixing.ih.module.system.pojo.User;
import com.chixing.ih.module.system.pojo.dto.UserDTO;
import com.chixing.ih.module.system.pojo.dto.UserRegisterDTO;
import com.chixing.ih.module.system.service.IUserService;
import com.chixing.ih.util.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 刘岩
 * @version 1.0
 * @Desc Practice makes perfect
 * @date 2020/9/21 22:17
 */
@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    WorkMapper workMapper;

    @Autowired
    ProCommunityMapper proCommunityMapper;

    @Autowired
    ImageMapper imageMapper;


    @Override
    public boolean saveUser(UserRegisterDTO userRegisterDTO) {
        String userPassword = userRegisterDTO.getUserPassword();
        String encrypt = EncryptUtil.encrypt(userPassword, null);
        userRegisterDTO.setUserPassword(encrypt);
        userRegisterDTO.setRoleId(3);
        userRegisterDTO.setUpdateTime(new Date());
        int record = userMapper.insertUser(userRegisterDTO);
        if (record > 0) {
            workMapper.insertWork(userRegisterDTO.getUserId());
            proCommunityMapper.insertUserId(userRegisterDTO.getUserId());
            UserImage userImage = new UserImage();
            userImage.setUserId(userRegisterDTO.getUserId());
            userImage.setImageUrl("https://masteryan.oss-cn-beijing.aliyuncs.com/1000000740998476.png");
            imageMapper.insertUserOne(userImage);
            return true;
        } else {
            return false;
        }

    }

    @Override
    public boolean isAccountExisted(String userAccount) {
        User userByAccount = userMapper.getUserByAccount(userAccount);
        if (userByAccount == null) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public boolean isNickNameExisted(String userNickName) {
        User userByNickName = userMapper.getUserByNickName(userNickName);
        if (userByNickName == null) {
            return true;
        } else {
            return false;
        }
    }


    @Override
    public UserDTO isLoginSuccess(User user) {
        user.setUserPassword(EncryptUtil.encrypt(user.getUserPassword(), null));
        User userMapperUser = userMapper.getUser(user);
        if(userMapperUser==null)
        return null;
        UserImage oneByUserId = imageMapper.getOneByUserId(userMapperUser.getUserId());
        UserDTO userDTO = new UserDTO();
        userDTO.setUser(userMapperUser);
        userDTO.setUserImage(oneByUserId);
        return userDTO;

    }
}
